package com.quectel.core.module.application.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.quectel.constant.global.SystemConstants;
import com.quectel.core.constants.RedisCacheConstants;
import com.quectel.core.module.application.dao.ApplicationAccessConfDao;
import com.quectel.core.module.application.dto.ApplicationAccessConfDto;
import com.quectel.core.module.application.entity.ApplicationAccessConfEntity;
import com.quectel.core.module.application.service.ApplicationAccessConfService;
import com.quectel.util.common.CopyUtils;
import com.quectel.util.kit.CacheKit;
import com.quectel.util.kit.Snowflake;
import com.quectel.util.mybatis.MyBatisPlusUtils;
import org.apache.dubbo.config.annotation.DubboService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * @author rananxin
 * @date 2021-11-19 14:20:25
 */
@DubboService
public class ApplicationAccessConfServiceImpl implements ApplicationAccessConfService {

    private static final Logger LOGGER = LoggerFactory.getLogger(ApplicationAccessConfServiceImpl.class);

    private static final String APPLICATION_ACCESS_CONF_CACHE_BY_ID = RedisCacheConstants.PROJECT_NAME + "APPLICATION_ACCESS_CONF_CACHE_BY_ID:";

    @Autowired
    private ApplicationAccessConfDao applicationAccessConfDao;


    @Override
    public ApplicationAccessConfDto selectByCode(String code) {
        ApplicationAccessConfEntity applicationAccessConfEntity = applicationAccessConfDao
                .selectOne(new LambdaQueryWrapper<ApplicationAccessConfEntity>()
                        .eq(ApplicationAccessConfEntity::getCode, code));
        return CopyUtils.copyObj(applicationAccessConfEntity, ApplicationAccessConfDto.class);
    }

    @Override
    public ApplicationAccessConfDto selectCacheById(Long id) {
        return CacheKit.cacheToRedis(
                () -> selectById(id),
                APPLICATION_ACCESS_CONF_CACHE_BY_ID + id,

                SystemConstants.NOT_NULL_CACHE_EXPIRE_SECONDS
        );
    }

    @Override
    public ApplicationAccessConfDto selectById(Long id) {
        ApplicationAccessConfEntity entity = applicationAccessConfDao.selectById(id);
        return paddingField(CopyUtils.copyObj(entity, ApplicationAccessConfDto.class));
    }

    @Override
    public List<ApplicationAccessConfDto> queryList(Map<String, Object> params) {
        List<ApplicationAccessConfEntity> list = applicationAccessConfDao.queryList(params);
        List<ApplicationAccessConfDto> result = CopyUtils.copyList(list, ApplicationAccessConfDto.class);
        result.forEach(this::paddingField);
        return result;
    }

    private ApplicationAccessConfDto paddingField(ApplicationAccessConfDto dto) {
        if (dto != null) {

        }
        return dto;
    }

    @Override
    public int queryTotal(Map<String, Object> params) {
        return applicationAccessConfDao.queryTotal(params);
    }

    @Override
    public Long save(ApplicationAccessConfDto dto) {
        ApplicationAccessConfEntity entity = CopyUtils.copyObj(dto, ApplicationAccessConfEntity.class);
        entity.setId(Snowflake.nextId());
        applicationAccessConfDao.insert(entity);
        return entity.getId();
    }

    @Override
    public void updateById(ApplicationAccessConfDto dto) {
        ApplicationAccessConfEntity entity = CopyUtils.copyObj(dto, ApplicationAccessConfEntity.class);
        applicationAccessConfDao.updateById(entity);
        CacheKit.invalidRedisCache(APPLICATION_ACCESS_CONF_CACHE_BY_ID + dto.getId());
    }

    @Override
    public void updateAllColumnById(ApplicationAccessConfDto dto) {
        ApplicationAccessConfEntity entity = CopyUtils.copyObj(dto, ApplicationAccessConfEntity.class);


        MyBatisPlusUtils.updateAllColumnById(entity, applicationAccessConfDao);

        CacheKit.invalidRedisCache(APPLICATION_ACCESS_CONF_CACHE_BY_ID + dto.getId());
    }

    @Override
    public void deleteById(Long id) {
        applicationAccessConfDao.deleteById(id);

        CacheKit.invalidRedisCache(APPLICATION_ACCESS_CONF_CACHE_BY_ID + id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteBatch(Long[] ids) {
        for (Long id : ids) {
            deleteById(id);
        }
    }
}
